<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /pro/default.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	public $sqlplus = "";
	public $qs = '';
	public $pageno = 1;
	public $pagenumber = 0;
	public $pagearr = array();
	
	public $_cateId = false; 
	public $_factId = false;
	
	function __construct(){
		global $config;
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
		
		if(isset($_GET['page'])){
			$this->pageno = $_GET['page'];
		}
		if(isset($_GET['cateId'])){
			$this->_cateId = $_GET['cateId'];
		}
		if(isset($_GET['factId'])){
			$this->_factId = $_GET['factId'];
		}
		
		if($this->_factId == true && $this->_cateId != true  ){
			$this->sqlplus = " WHERE cs2_prod.factId =".$this->_factId." ";
			$this->qs = "&factId=".$this->_factId;
		}elseif($this->_cateId == true && $this->_factId != true  ){
			$this->sqlplus = " WHERE cs2_prod.cateId =".$this->_cateId."  ";
			$this->qs = "&cateId=".$this->_cateId;
		}elseif($this->_cateId == true && $this->_factId == true  ){
			$this->sqlplus = " WHERE cs2_prod.cateId =".$this->_cateId." AND cs2_prod.factId =".$this->_factId." ";
			$this->qs = "&factId=".$this->_factId."&cateId=".$this->_cateId;
		}
		$config->smarty->assign('page', $this->pageno);
		$config->smarty->assign('qs', $this->qs);
	}
	function len(){
		global $config;
		$l = 0;

		$sql = "SELECT COUNT(*) as cnt FROM cs2_prod ";
		$sql = $sql.$this->sqlplus;
		$row = $this->database->get_one($sql);
		
		$l = $row['cnt'];
		
		$this->pagenumber = $l / config::conf_web_pagecount;
		
		$config->smarty->assign('pagenumber', $this->pagenumber);
		
		for($i=1; $i<$this->pagenumber + 1; $i++){
			array_push($this->pagearr,$i);
		}
		$config->smarty->assign('pagearr', $this->pagearr);

		return $l;
	}
	
	function listview(){
		$lv = null;
		try{
			$sql = "SELECT cs2_prod.prodId , cs2_prod.name, cs2_prod.model, 
				cs2_prod.factId, cs2_prod.cateId, cs2_prod.info,cs2_prod.regdate,
				cs2_prod.size_l,cs2_prod.size_w,cs2_prod.size_h,cs2_prod.weight,
				cs2_prod.guid,cs2_fact.name as factname,cs2_cate.name as catename,
				IFNULL((SELECT cs2_image.fileId FROM cs2_image 
					WHERE cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image') AS fileId
				FROM cs2_prod  LEFT JOIN (cs2_fact,cs2_cate)
				ON (cs2_fact.factId=cs2_prod.factId AND cs2_cate.cateId = cs2_prod.cateId)";
			$sql = $sql.$this->sqlplus;
			$sql = $sql." ORDER BY prodId DESC LIMIT " . ($this->pageno-1) * config::conf_web_pagecount ." , ".config::conf_web_pagecount." ;";
			
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function category_list_top(){
		$lv = null;
		$sql = "SELECT cateId,name FROM cs2_cate WHERE (rootId=1) ";
		try {
			$lv = $this->database->select($sql);
		} catch (Exception $e) {
		}
		return $lv;
	}
	function factory_list(){
		$lv = null;
		$sql = "SELECT factId,name FROM cs2_fact ORDER BY name";
		try {
			$lv = $this->database->select($sql);
		} catch (Exception $e) {
			
		}
		return $lv;
	}
	function ajax_callback(){
		global $config;
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_ADD_PROD){
			$name = $_POST['name'];
			$model = $_POST['model'];
			$factId = $_POST['factId'];
			$cateId = $_POST['cateId'];
			$info = $_POST['info'];
			$size_l = $_POST['size_l'];
			$size_h = $_POST['size_h'];
			$size_w = $_POST['size_w'];
			$weight = $_POST['weight'];
			$guid = $config->guid();
			
			$sql = "INSERT INTO cs2_prod (name, model, factId, cateId, 
				info,regdate,size_l,size_w,size_h,weight,guid,barcode ) 
			VALUES ('".$name."','".$model."',".$factId.",".$cateId.",'".$info.
				"',NOW(),".$size_l.",".$size_w.",".$size_h.",".$weight.",'".$guid."','');";
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_PROD){
			$prodId = $_POST['prodId'];
			$sql = "DELETE FROM cs2_prod WHERE (prodId=".$prodId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_PROD){
			$name = $_POST['name'];
			$model = $_POST['model'];
			$factId = $_POST['factId'];
			$cateId = $_POST['cateId'];
			$info = $_POST['info'];
			$size_l = $_POST['size_l'];
			$size_h = $_POST['size_h'];
			$size_w = $_POST['size_w'];
			$weight = $_POST['weight'];
			$prodId = $_POST['prodId'];
		
			$sql = "UPDATE cs2_prod SET 
				name='".$name."' ,
				model='".$model."' ,
				factId=".$factId." ,
				cateId=".$cateId." ,
				info='".$info."' ,
				size_l=".$size_l." ,
				size_h=".$size_h." ,
				size_w=".$size_w." ,
				weight=".$weight;
			
			$sql = $sql." WHERE (prodId =". $prodId.");";
			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK","name"=>$name,"prodId"=>$prodId);
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_LOAD_CATEGORY){
			$cateId = $_POST['cateId'];
			$parent = $_POST['parent'];
			$sql = "SELECT count(*) cnt FROM cs2_cate WHERE (rootId=".$cateId.") ";
			$row = $this->database->get_one($sql);
			if($row['cnt'] != 0){
				$sql = "SELECT cateId,name FROM cs2_cate WHERE (rootId=".$cateId.") ";
				try {
					$rows = $this->database->select($sql);
					$json = array("ref"=>"OK","rootId"=>$cateId, "parent"=>$parent,"rows"=>$rows);
				} catch (Exception $e) {
					$json = array("ref"=>"error,database.");
				}
			}else{
				$json = array("ref"=>"NOCHILD");
			}
		}
		if($token == config::TOKEN_LOAD_IMAGES){
			$prodId = $_POST['prodId'];
			$sql = "SELECT count(*) cnt FROM cs2_image WHERE (prodId=".$prodId.") ";
			$row = $this->database->get_one($sql);
			if($row['cnt'] != 0){
				$sql = "SELECT imageId,fileId,prodId,defId FROM cs2_image WHERE (prodId=".$prodId.") ";
				try {
					$rows = $this->database->select($sql);
					$json = array("ref"=>"OK","rows"=>$rows);
				} catch (Exception $e) {
					$json = array("ref"=>"error,database.");
				}
			}else{
				$json = array("ref"=>"NOIMAGE");
			}
		}
		return json_encode($json);
	}
}
$pg = new page_index();
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	
	$config->smarty->assign('pagecount', config::conf_web_pagecount);
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	$config->smarty->assign('category_list_top', $pg->category_list_top());
	$config->smarty->assign('factory_list', $pg->factory_list());

	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}
